<?php
class ArticleAction extends Action {
	
	//文章点击数加1
	public function totalAdd(){
		$article = M('articles');
		$article->where('id='.$_POST['articleId'])->setInc('total'); 
	}
	
	//文章顶
	public function goodVoteAdd(){
		$article = M('articles');
		$linkID = $article->where('id='.$_POST['articleId'])->setInc('goodVote'); 
		
		$this->ajaxReturn($linkID,"",$linkID);
	}
		
	//文章踩
	public function badVoteAdd(){
		$article = M('articles');
		$linkID = $article->where('id='.$_POST['articleId'])->setDec('badVote'); 
		
		$this->ajaxReturn($linkID,"",$linkID);
	}
	
	//根据栏目id删除文章
	public function deleteByChanId($channelId){
		$article = M('articles');
		$linkID = $article->where('chanId='.$_POST['$channelId'])-> delete(); 
	}
	
	//批量根据id删除文章
	public function deleteById(){
		$article = M('articles');
		$article->where('id in ('.implode($_POST['articleId']).')')-> delete(); 
	}
	
	//文章插入，传参方便采集模块导入数据
	public function insert($data){
		$data = empty($data)? array(
				id => String::keyGen(),
				chanId  => $_POST['channelId'],
				title => $_POST['title'],
				author => empty($_POST['author'])?'侠名':$_POST['author'],
				content => $_POST['content'],
				datatime  => time(),
				goodVote => 0,
				badVote => 0,
				total  => 0
			) : $data;
		
		$article = M('articles');
		$article-> add($data); 
	}
	
	//文章更新
	public function update(){
		$chanId = $_POST['channelId'];
		$title = $_POST['title'];
		$author = $_POST['author'];
		$content = $_POST['content'];
		
		$article = M('articles');
		$data = array();
		
		if($chanId){
			array_push($data, $chanId);
		}else if($title){
			array_push($data, $title);
		}else if($author){
			array_push($data, $author);
		}else if($content){
			array_push($data, $content);
		}
		
		$linkID = $article-> where('id='.$_POST['articleId'])->save($data); 
	}
	
	//根据文章id查旬文章
	public function selectById(){
		$article = M('articles');
		$data = $article-> where('id='.$_POST['articleId'])-> select(); 
		
		$this->ajaxReturn($data?TRUE:FALSE,"",$data);
	}
	
	//根据栏目id查询最新的前?条数据
	public function selectById(){
		$channelId = $_POST['channelId'];
		$article = M('articles');
		
		$channel = A('Channel');
		
		$childs = $channel -> getByIdChilds();
		
		$data = $article-> where('chanId in('.implode($childs).')') -> order('datatime')
				-> limit($_POST['count'])->select();
		
		$this->ajaxReturn($data?TRUE:FALSE,"",$data);
	}
	
	//标题搜索,带分页
	public function selectSearch(){
		$article = M('articles');
		//重点！'title'为提交页面的post变量，一定要用$_REQUEST
		$map = Array ('title' => $_REQUEST ['title'] ); 
		$count = $article-> where($map)->count();          // 查询满足要求的总记录数
		$Page = new Page($count,$_POST['count']);          // 实例化分页类 传入总记录数和每页显示的记录数
		$list = $article->where($map )->limit ( $Page->firstRow . ',' . $Page->listRows )->select (); 
		
		//分页跳转的时候保证查询条件
		 foreach($map as $key=>$val) {
			$Page -> parameter .= "$key=".urlencode($val)."&";
		 }
		$show = $Page->show();                             // 分页显示输出
		$this->assign('list',$list);                       // 赋值数据集
		$this->assign('page',$show);                       // 赋值分页输出
	}
	
	//文章分页
	public function selectLimitList(){
		$article = M('articles');
		$count = $article ->count();                      //得到总数
		$Page = new Page($count,$_POST['count']);         // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show();                            // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $article->where('status=1')->order('datatime')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('list',$list);                      // 赋值数据集
		$this->assign('page',$show);                      // 赋值分页输出
	}
	
}
?>